package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.annotation.HttpConstraint;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R Employeelogin(HttpServletRequest request, @RequestBody Employee employee) {

        //1、将页面提交的密码password进行md5加密处理
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername, employee.getUsername());

        Employee one = employeeService.getOne(wrapper);

        if (one == null) {
            return R.error("账号或密码错误");//账号不存在
        }

        if (!password.equals(one.getPassword())) {

            return R.error("账号或密码错误");//密码错误
        }
        if (one.getStatus() == 0) {
            return R.error("账号已被封禁"); //账号封禁
        }

        //6、登录成功，将员工id存入Session并返回登录成功结果
        request.getSession().setAttribute("employee", one.getId());
        return R.success(one);
    }

    @PostMapping("/logout")
    private R<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");

        return R.success("退出成功");
    }

    @GetMapping("/page")
    private R<Page> getpage(int page, int pageSize, String name) {

        Page page1 = new Page(page, pageSize);
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();

        wrapper.like(StringUtils.isNotEmpty(name), Employee::getName, name);

        Page page2 = employeeService.page(page1, wrapper);

        return R.success(page2);
    }

    @PostMapping
    private R<String> adde(@RequestBody Employee employee) {

//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//
//        employee.setPassword(DigestUtils.md5DigestAsHex("12345".getBytes()));
//
//
//        Long employee1 = (Long) request.getSession().getAttribute("employee");
//
//        employee.setUpdateUser(employee1);
//        employee.setCreateUser(employee1);


        employeeService.save(employee);

        return R.success("添加成功");

    }

    @PutMapping
    private R<String> jinyong(@RequestBody Employee employee) {

//        Long empId = (Long) request.getSession().getAttribute("employee");
//
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(empId);
        employeeService.updateById(employee);

        return R.success("操作成功");
    }


    @GetMapping("/{id}")
    private R<Employee> getone(@PathVariable Long id){
        Employee byId = employeeService.getById(id);
        return R.success(byId);

    }

}
